package panels;

import game.Game;
import game.GameDriver;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import player.Alien;
import player.Flapper;
import player.Human;
import player.Player;
import player.Race;

public class PlayerConfigurationPanel extends JPanel {

    /** Auto-generated serial version ID. */
    private static final long serialVersionUID = 6506703421207942892L;
    private Game currentGame;
    @SuppressWarnings("unused")
    private GameDriver driver;
    private Player currentPlayer;
    private int playerNumber;
    private String playerName;
    private Race race;
    private Color color;
    private JPanel main, name, title, races, colors;
    private JLabel playerConfig, nameLabel, colorLabel, raceLabel;
    private JComboBox racesList, colorsList;
    private JTextField nameField;
    private String[] raceStrings = { "Human (hardest)", "Flapper (easiest)",
	    "Bonzoid", "Ugaite", "Buzzite" };
    private String[] colorStrings = { "Red", "Green", "Blue", "Yellow" };

    public PlayerConfigurationPanel(Game currentGame, GameDriver driver,
	    int playerNumber) {

	this.currentGame = currentGame;
	this.driver = driver;
	this.playerNumber = playerNumber;

	playerConfig = new JLabel(new ImageIcon(this.getClass().getResource("/images/player"+(playerNumber+1)+".png")));
	nameLabel = new JLabel(new ImageIcon(this.getClass().getResource("/images/name.png")));
	colorLabel = new JLabel(new ImageIcon(this.getClass().getResource("/images/color.png")));
	raceLabel = new JLabel(new ImageIcon(this.getClass().getResource("/images/race.png")));

	main = new JPanel(); // create main panel to hold other panels
	main.setLayout(new GridLayout(4, 1, 0, 10));

	title = new JPanel();
	title.setLayout(new GridLayout(2, 1));
	title.setOpaque(false);
	title.add(new JLabel());
	title.add(playerConfig);

	name = new JPanel(); // create name panel
	name.setLayout(new GridLayout(2, 1));
	name.add(nameLabel);

	// remove constructor parameter eventually
	// text field for user to type in name
	nameField = new JTextField("Player"
		+ (Math.round(Math.random() * 100) - 1));
	name.add(nameField);
	name.setOpaque(false);


	races = new JPanel(); // create panel to hold races
	racesList = new JComboBox(raceStrings); // races dropdown
	racesList.setSelectedIndex(0); // sets default race to index 0
	races.setLayout(new GridLayout(2, 1));
	races.setOpaque(false);

	races.add(raceLabel);
	races.add(racesList);

	colors = new JPanel(); // create panel to hold colors
	colorsList = new JComboBox(colorStrings); // colors dropdown
	colorsList.setSelectedIndex(playerNumber); // sets default color to index 0
	colors.setLayout(new GridLayout(2, 1));
	colors.setOpaque(false);
	colors.add(colorLabel);
	colors.add(colorsList);

	// set gridlayout with padding
	main.setLayout(new GridLayout(1, 5, 10, 10));

	// add all panels to main panel
	main.add(title);
	main.add(name);
	main.add(races);
	main.add(colors);
	main.setOpaque(false);

	add(main);
	setBackground(new Color(112,164,96));

	setPreferredSize(new Dimension(currentGame.getWidth(), 100));
    }

    public boolean givePlayerInformationToGame() {
	playerName = nameField.getText(); // get text from name textfield
	if (nameField.getText().trim().equals("")) { // if it's blank
	    JOptionPane.showMessageDialog(null, "Player " + (playerNumber + 1)
		    + ", " + "Choose a valid name.");
	    return false;
	} else {
	    int raceIndex = racesList.getSelectedIndex();
	    // switch for player's race
	    switch (raceIndex) {
	    case 0:
		race = new Human();
		break;
	    case 1:
		race = new Flapper();
		break;
	    default:
		race = new Alien();
		break;
	    }
	    int colorIndex = colorsList.getSelectedIndex();
	    // switch for player's color
	    switch (colorIndex) {
	    case 0:
		color = Color.red;
		break;
	    case 1:
		color = Color.green;
		break;
	    case 2:
		color = Color.cyan;
		break;
	    case 3:
		color = Color.yellow;
		break;
	    default:
		color = Color.red;
		break;
	    }

	    // initializes current Player using selected information
	    currentPlayer = new Player(currentGame, playerName, color,
		    colorStrings[colorIndex], race);
	    currentGame.setPlayer(currentPlayer, playerNumber);

	    return true;
	}
    }
}